package com.ccdt.tv.smartrecommend.view.bean;

import android.content.Intent;
import android.util.Log;

import com.ccdt.tv.smartrecommend.utils.ReflectHelper;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by wudz on 2017/1/16.
 */
public class IntentEventBean extends OuterEventBean {
    public static final int TYPE_STARTACTIVITY = 0;
    public static final int TYPE_STARTSERVICE = 1;
    public static final int TYPE_SENDBROADCAST = 2;
    public static final int TYPE_COMMAND = 3;

    private static String TAG = "IntentEventBean";
    private static Gson gson = new Gson();
    private static HashMap<String, Integer> flagFieldMap;

    static {
        flagFieldMap = ReflectHelper.getStaticFieldMap_Int("android.content.Intent", "FLAG_ACTIVITY");
    }

    private int type; //0 startActivity 1 startService 2 sendBroadCast 3 command line (am onStartDraw)
    private String action;
    private String dataUri;
    private String packageName;
    private String className;
    private String mimeType;
    private HashMap<String, String> keyValue = new HashMap<>();
    private HashMap<String, Integer> keyValueInt = new HashMap<>();
    private HashMap<String, Boolean> keyValueBoolean = new HashMap<>();
    private ArrayList<String> flag = new ArrayList<>();
    private ArrayList<String> category = new ArrayList<>();


    private IntentEventBean(IntentEventBean p) {
        super(p.getEventId(), p.getSrcViewId(), p.getSrcMethodName());
        this.type = p.type;
        this.action = p.action;
        this.dataUri = p.dataUri;
        this.mimeType = p.mimeType;
        this.packageName = p.packageName;
        this.flag = p.flag;
        this.className = p.className;
        this.keyValue = new HashMap<>();
        this.keyValue.putAll(p.keyValue);
        this.keyValueInt = new HashMap<>();
        this.keyValueInt.putAll(p.keyValueInt);
        this.keyValueBoolean = new HashMap<>();
        this.keyValueBoolean.putAll(p.keyValueBoolean);
    }

    public static IntentEventBean getBeanFromJson(String json) {
        return gson.fromJson(json, IntentEventBean.class);
    }

    public IntentEventBean(Builder b) {
        super(b.eventId, b.srcViewId, b.srcMethodName);
        this.type = b.startType;
        this.action = b.action;
        this.packageName = b.packageName;
        this.className = b.className;
        this.mimeType = b.commandLine;
        this.dataUri = b.dataUri;
        this.flag = getActiviyFlagStr(b.activityFlag);
        this.keyValue.putAll(b.keyvalue);
        this.keyValueInt.putAll(b.keyvalueInt);
    }

    public static class Builder {
        private String eventId;
        private String srcViewId;
        private String srcMethodName;
        private int startType;
        private String dataUri;
        private String action;
        private String commandLine;
        private String packageName;
        private String className;
        private HashMap<String, String> keyvalue = new HashMap<>();
        private HashMap<String, Integer> keyvalueInt = new HashMap<>();
        private HashMap<String, Boolean> keyvalueBool = new HashMap<>();
        private int activityFlag = Intent.FLAG_ACTIVITY_NEW_TASK;

        public Builder(String eventId, String srcViewId, String srcMethodName, int startType) {
            this.eventId = eventId;
            this.srcViewId = srcViewId;
            this.srcMethodName = srcMethodName;
            this.packageName = "";
            this.className = "";
            this.startType = startType;
            this.action = "";
            this.dataUri = "";
            this.commandLine = "";
        }

        public Builder setPackageName(String packageName) {
            this.packageName = packageName;
            return this;
        }

        public Builder setClassName(String className) {
            this.className = className;
            return this;
        }

        public Builder addKeyValue(String keyStr, String valueStr) {
            keyvalue.put(keyStr, valueStr);
            return this;
        }

        public Builder addKeyValueInt(String keyStr, int valueInt) {
            keyvalueInt.put(keyStr, valueInt);
            return this;
        }

        public Builder addKeyValueBool(String keyStr, boolean valueBool) {
            keyvalueBool.put(keyStr, valueBool);
            return this;
        }

        public Builder setIntentFlag(int flag) {
            this.activityFlag = flag;
            return this;
        }

        public Builder setDataUri(String dataUri) {
            this.dataUri = dataUri;
            return this;
        }

        public Builder setCommandline(String commandline) {
            this.commandLine = commandline;
            return this;
        }

        public Builder setAction(String action) {
            this.action = action;
            return this;
        }

        public IntentEventBean build() {
            return new IntentEventBean(this);
        }
    }

    public int getStartType() {
        return type;
    }

    public String getAction() {
        return action;
    }

    public String getDataUri() {
        return dataUri;
    }

    public String getCommandLine() {
        return mimeType;
    }

    public String getPackageName() {
        return packageName;
    }

    public String getClassName() {
        return className;
    }

    public HashMap<String, String> getKeyValue() {
        return keyValue;
    }

    public HashMap<String, Integer> getKeyValueInt() {
        return keyValueInt;
    }

    public HashMap<String, Boolean> getKeyValueBoolean() {
        return keyValueBoolean;
    }

    public int getFlag() {
        Log.i(TAG, "activityFlag:" + flag);
        int aflag = getActivityFlagInt(flag);
        if (aflag == 0)
            aflag = Intent.FLAG_ACTIVITY_NEW_TASK;
        return aflag;
    }

    public ArrayList<String> getCategory() {
        return category;
    }

    public void setCategory(ArrayList<String> category) {
        this.category = category;
    }

    @Override
    public IntentEventBean clone() {
        try {
            IntentEventBean o = new IntentEventBean(this);
            Log.i("IntentEventBean", "" + o);
            return o;
        } catch (Exception e) {
            return null;
        }
    }

    public String toJson() {
        return new Gson().toJson(this);
    }

    private ArrayList<String> getActiviyFlagStr(int flag) {
        ArrayList<String> intentString = new ArrayList<>();
        for (String getKey : flagFieldMap.keySet()) {
            if ((flagFieldMap.get(getKey).intValue() & flag) != 0) {
                intentString.add(getKey);
            }
        }
        return intentString;
    }

    private int getActivityFlagInt(ArrayList<String> str) {
        int res = 0;
        if (null != str && str.size() > 0) {
            for (String s : str) {
                res = res | flagFieldMap.get(s).intValue();
            }
        }
        return res;
    }

    @Override
    public String toString() {
        return "IntentEventBean{" +
                "startType=" + type +
                ", action='" + action + '\'' +
                ", dataUri='" + dataUri + '\'' +
                ", packageName='" + packageName + '\'' +
                ", className='" + className + '\'' +
                ", commandLine='" + mimeType + '\'' +
                ", keyValue=" + keyValue +
                ", keyValueInt=" + keyValueInt +
                ", keyValueBoolean=" + keyValueBoolean +
                ", activityFlag=" + flag +
                '}' + super.toString();
    }


}
